<?php
namespace Admin\Controller;
use Think\Controller;
use Myself\FileSystem;
use Admin\Controller\CommonController;
class OrderController extends CommonController {

	protected $statusArr=array("下架","上架");
	protected $isHome=array("未推荐","已推荐");

    public function index(){
		header('Content-Type:text/html;charset=utf-8');
             
		//-----------------搜索-------------------
		
                
                 $order_no = I('order_no','');
                 if($order_no){
                           $where['order_no']= $order_no;
                           $shArray['order_no'] =$order_no;
                  }
                  
                  
		
                 $telephone = I('telephone','');
                 if($telephone){
                     
                       $dataMsg = M('shop_detail')->where('telephone="'.$telephone.'"')->find();
                       if($dataMsg){
                           $mid = M('shop')->where('id="'.$dataMsg['s_id'].'"')->getField('mid');
                         
                       }
                           $where['from_mid'] = $mid;
                           $shArray['telephone'] =$telephone;
                  }
                 
                  
                  
                 $shop_name = I('shop_name','');
                 if( $shop_name){
                     
                       $dataMsg = M('shop')->where('shop_name="'.$shop_name.'"')->find();
                       if($dataMsg){
                           $mid = M('shop')->where('mid="'.$dataMsg['mid'].'"')->getField('mid');
                          
                       }
                        $where['from_mid'] = $mid;
                           $shArray['shop_name'] =$shop_name;
                  }
                  
        
		$from_time = I('from_time','');
		$to_time = I('to_time','');
		  
		if($from_time){
			$from_time_s = strtotime($from_time);
			$where['add_time']= array('EGT',$from_time_s);
			$shArray['from_time'] = $from_time;
		}
		
		if($to_time){
			$to_time_s = strtotime($to_time)+86400;
			$where['add_time']= array('ELT',$to_time_s);
			$shArray['to_time'] = $to_time;	
		}		
		
		if($from_time && $to_time){
			$where['add_time']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
		}	
			  
		if(!empty($shArray)){$this->assign('shArray',$shArray);}// 搜索参数输出
		//-------------------------搜索end-----------------

		$pageno = I('p',1);
                $pageCount = 10;
            
		$list = M('shop_product_order')->where($where)->order('add_time desc, id desc')->page($pageno.','.$pageCount)->select();
		//echo M('shop_product_order')->getlastsql();exit;
                //订单状态（店内消费：待付款、等待接单、等待上餐、已取消、已完成、退款中）
                //
                //订单状态（校边外卖：待付款、等待接单、等待配送、正在配送、已取消、已完成、退款中）
                //
                //订单状态（影院商家：待付款、等待出票、已完成、已取消、退款中)

                foreach($list as $key=>$item){
                  if($list[$key]['cost_type'] == 1){
                      
                            $list[$key]['cost_type_name']='店内消费1';
                            if( $list[$key]['order_status'] == 1){
                                $list[$key]['order_status_value'] = '待付款1';
                                $list[$key]['pay_status_value'] = '未支付';
                            }elseif ($list[$key]['order_status'] == 2){
                                $list[$key]['order_status_value'] = '等待接单1';
                            }elseif ($list[$key]['order_status'] == 3){
                                $list[$key]['order_status_value'] = '等待上餐1';
                            }elseif ($list[$key]['order_status'] == 8){
                                $list[$key]['order_status_value'] = '已取消1';
                                 $list[$key]['pay_status_value'] = '已退款';
                            }elseif ($list[$key]['order_status'] == 9){
                                $list[$key]['order_status_value'] = '已完成1';
                            }elseif ($list[$key]['order_status'] == 10){
                                $list[$key]['order_status_value'] = '退款中1';
                            }else {
                            }
                 }elseif($list[$key]['cost_type'] == 2) {
                            $list[$key]['cost_type_name']='校边外卖2';
                            if( $list[$key]['order_status'] == 1){
                                $list[$key]['order_status_value'] = '待付款2';
                                $list[$key]['pay_status_value'] = '未支付';
                            }elseif ($list[$key]['order_status'] == 2){
                                $list[$key]['order_status_value'] = '等待接单2';
                            }elseif ($list[$key]['order_status'] == 3){
                                $list[$key]['order_status_value'] = '等待配送2';
                                
                            }elseif ($list[$key]['order_status'] == 4){
                                $list[$key]['order_status_value'] = '正在配送2';
                                
                            } elseif ($list[$key]['order_status'] == 8){
                                $list[$key]['order_status_value'] = '已取消2';
                                $list[$key]['pay_status_value'] = '已退款';
                            }elseif ($list[$key]['order_status'] == 9){
                                $list[$key]['order_status_value'] = '已完成2';
                            }elseif ($list[$key]['order_status'] == 10){
                                $list[$key]['order_status_value'] = '退款中2';
                            }else {
                            }
                  }  else {
                            $list[$key]['cost_type_name']='商家影院3';
                            if( $list[$key]['order_status'] == 1){
                                $list[$key]['order_status_value'] = '待付款3';
                                 $list[$key]['pay_status_value'] = '未支付';
                            }elseif ($list[$key]['order_status'] == 2){
                                $list[$key]['order_status_value'] = '等待出票3';
                            }elseif ($list[$key]['order_status'] == 8){
                                $list[$key]['order_status_value'] = '已取消3';
                                $list[$key]['pay_status_value'] = '已退款';
                                
                            }elseif ($list[$key]['order_status'] == 9){
                                $list[$key]['order_status_value'] = '已完成3';
                            }elseif ($list[$key]['order_status'] == 10){
                                $list[$key]['order_status_value'] = '退款中3';
                            }else {
                            }
                   }
		 $list[$key]['pro_id'] = M('shop_product_order_pro')->where('o_id="'.$item['id'].'"')->getField('pro_id');
                 $list[$key]['pro_name'] = M('shop_product')->where('id="'.$list[$key]['pro_id'].'"')->getField('pro_name'); 	
                 $list[$key]['phone'] =M('customer')->where('cid="'.$item['to_cid'].'"')->getField('phone');
                 $list[$key]['address1'] = M('shop_product_order_pro')->where('o_id="'.$item['id'].'"')->getField('address1'); 
                 $list[$key]['cus_id'] =M('customer')->where('cid="'.$item['to_cid'].'"')->getField('id');
                 $list[$key]['fullname'] = M('customer_detail')->where('c_id="'.$list[$key]['cus_id'].'"')->getField('fullname');
                 $list[$key]['shop_name'] =M('shop')->where('mid="'.$item['from_mid'].'"')->getField('shop_name');
                 $list[$key]['shop_id'] =M('shop')->where('mid="'.$item['from_mid'].'"')->getField('id');
                 $list[$key]['telephone'] = M('shop_detail')->where('s_id="'.$list[$key]['shop_id'].'"')->getField('telephone');
                  
              }
                
              
                $this->assign('list',$list);
		$count = M('shop_product_order')->where($where)->count();
		$this->assign('count',$count);
		$Page = new \Think\Page($count,$pageCount);
		$show = $Page->showAdmin();
		$this->assign('page',$show);
                $this->assign('statusArr',$this->statusArr);
		$this->display('Order/index');
    }

 
	
	public function editOrder(){
		header('Content-Type:text/html;charset=utf-8');
	         $id = I('id',0,'int');
		 $dataInfo = M('shop_product_order')->find($id);	
                
                
                         if($dataInfo['cost_type'] == 1){
                      
                         
                            if( $dataInfo['order_status'] == 1){
                                $dataInfo['order_status_value'] = '待付款1';
                             
                            }elseif ($list[$key]['order_status'] == 2){
                                $dataInfo['order_status_value'] = '等待接单1';
                            }elseif ($dataInfo['order_status'] == 3){
                                $dataInfo['order_status_value'] = '等待上餐1';
                            }elseif ($dataInfo['order_status'] == 8){
                                $dataInfo['order_status_value'] = '已取消1';
                             }elseif ($dataInfo['order_status'] == 9){
                                $dataInfo['order_status_value'] = '已完成1';
                            }elseif ($dataInfo['order_status'] == 10){
                                $dataInfo['order_status_value'] = '退款中1';
                            }else {
                            }
                 }elseif($dataInfo['cost_type'] == 2) {
                           
                            if( $dataInfo['order_status'] == 1){
                                $dataInfo['order_status_value'] = '待付款2';
                                $dataInfo['pay_status_value'] = '未支付';
                            }elseif ($dataInfo['order_status'] == 2){
                                $dataInfo['order_status_value'] = '等待接单2';
                            }elseif ($dataInfo['order_status'] == 3){
                               $dataInfo['order_status_value'] = '等待配送2';
                                
                            }elseif ($dataInfo['order_status'] == 4){
                                $dataInfo['order_status_value'] = '正在配送2';
                                
                            } elseif ($dataInfo['order_status'] == 8){
                               $dataInfo['order_status_value'] = '已取消2';
                              
                            }elseif ($dataInfo['order_status'] == 9){
                                $dataInfo['order_status_value'] = '已完成2';
                            }elseif ($dataInfo['order_status'] == 10){
                                $dataInfo['order_status_value'] = '退款中2';
                            }else {
                            }
                  }  else {
                            
                            if( $dataInfo['order_status'] == 1){
                               $dataInfo['order_status_value'] = '待付款3';
                             
                            }elseif ($dataInfo['order_status'] == 2){
                                $dataInfo['order_status_value'] = '等待出票3';
                            }elseif ($dataInfo['order_status'] == 8){
                                $dataInfo['order_status_value'] = '已取消3';
                              
                            }elseif ($dataInfo['order_status'] == 9){
                               $dataInfo['order_status_value'] = '已完成3';
                            }elseif ($dataInfo['order_status'] == 10){
                                $dataInfo['order_status_value'] = '退款中3';
                            }else {
                            }
                   }
                
             
                 $dataInfo['pro_id'] = M('shop_product_order_pro')->where('o_id="'.$dataInfo['id'].'"')->getField('pro_id');
                 
                 $dataInfo['buy_count'] = M('shop_product_order_pro')->where('o_id="'.$dataInfo['id'].'"')->getField('buy_count');
                 
		 $dataInfo['pro_name'] = M('shop_product')->where('id="'.$dataInfo['pro_id'].'"')->getField('pro_name'); 	
                 
	         $dataInfo['phone'] =M('customer')->where('cid="'.$dataInfo['to_cid'].'"')->getField('phone');
                 
                 $dataInfo['address1'] = M('shop_product_order_pro')->where('o_id="'.$dataInfo['id'].'"')->getField('address1'); 
                 
                 $dataInfo['cus_id'] =M('customer')->where('cid="'.$dataInfo['to_cid'].'"')->getField('id');
                 $dataInfo['fullname'] = M('customer_detail')->where('c_id="'.$dataInfo['cus_id'].'"')->getField('fullname');
                 
                 $dataInfo['shop_name'] =M('shop')->where('mid="'.$dataInfo['from_mid'].'"')->getField('shop_name');
                 $dataInfo['shop_id'] =M('shop')->where('mid="'.$dataInfo['from_mid'].'"')->getField('id');
                 $dataInfo['telephone'] = M('shop_detail')->where('s_id="'.$dataInfo['shop_id'].'"')->getField('telephone');
                 $this->assign('dataInfo', $dataInfo);
	         $this->display('Order/edit');
    }	
          public function canel_order(){
                $id = I('cid');
                $pay_status  = I('pay_status');
                $cost_type =  I('cost_type');
                $order_status = I('order_status');
                
                if($pay_status == 2){
                   $data['info'] = '您的订单已支付，不可取消';
		   $this->ajaxReturn($data);  
                  }
              
                  
                 if($order_status == 8){
                   $data['info'] = '您的订单已完成，不可取消';
		   $this->ajaxReturn($data);  
                  }
                  
                   if($order_status == 9){
                   $data['info'] = '您的订单已取消，不可取消';
		   $this->ajaxReturn($data);  
                  }
               
              
              
          }
	  public function del(){

            $id = $_REQUEST['id'];
			
            if (is_array($id)){
               $where = 'id in('.implode(',', $id).')';
            } else {
               $where = 'id='.$id;
            }
	    $list = M('shop_job')->where($where)->delete();
	    $this->redirect('Job/index');
            
    }

	
	 
	 
	 
	  
}